/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.domain;

import org.hibernate.annotations.Proxy;
import org.xptracker.persistence.Identified;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name="blog_entries")
@Proxy(lazy=false)
public class BlogEntry extends Identified {
  private String content;
  private User creator;
  private Date createdDate = new Date();
  private Long storyId;
  private WikiContent wikiContent = new WikiContent();

  public BlogEntry() {
  }

  public BlogEntry(User creator, Story story, String content) {
    this.creator = creator;
    this.storyId = story.getIdLong();
    wikiContent.setContent(content);
  }

  @ManyToOne(targetEntity=User.class)
  public User getCreator() {
    return creator;
  }

  public void setCreator(User creator) {
    this.creator = creator;
  }

  @Lob
  @Column(length=65535, name="content")
  public String getOldContent() {
    return content;
  }

  public void setOldContent(String content) {
    this.content = content;
  }

  @Transient
  public String getContent() {
    final WikiContent content1 = getWikiContent();
    return content1 == null ? "" : content1.getContent();
  }

  @Transient
  public void setContent(String content) {
    if (getWikiContent() == null) {
      throw new RuntimeException("WikiContent may not be null!");
    }
    getWikiContent().setContent(content);
  }

  @Column(name="story_id")
  public Long getStoryId() {
    return storyId;
  }

  protected void setStoryId(Long storyId) {
    this.storyId = storyId;
  }

  @Column(name="created_date")
  public Date getCreatedDate() {
    return createdDate;
  }

  public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
  }

  @ManyToOne(cascade = CascadeType.ALL)
  @JoinColumn(name="wiki_content_id")
  public WikiContent getWikiContent() {
    if (wikiContent == null) {
      wikiContent = new WikiContent();
    }
    return wikiContent;
  }

  public void setWikiContent(WikiContent wikiContent) {
    this.wikiContent = wikiContent;
  }

  public String toString() {
    return "BlogEntry #" + getId() + " for Story " + storyId + " on " + createdDate;
  }
}
